package com.example.cofeed.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.cofeed.R;
import com.example.cofeed.adapter.HistoryAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.base.BaseListActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.History;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends BaseListActivity<History> {

    //    @BindView(R.id.rv_list)
//    RecyclerView mRvList;
    private HistoryAdapter mHistoryAdapter;

//    @Override
//    protected int getLayoutId() {
//        return R.layout.activity_history;
//    }

//    @Override
//    protected void init(Bundle savedInstanceState, View container) {
//        setMainTitle("浏览记录");
//        setSubTitle("清空记录");
//
//        mRvList.setLayoutManager(new LinearLayoutManager(this));
//        mHistoryAdapter = new HistoryAdapter();
//        mRvList.setAdapter(mHistoryAdapter);
//
//        getData();
//
//    }

    @Override
    protected void bindRecyclerView(RecyclerView recLv) {
        setMainTitle("浏览记录");
        setSubTitle("清空记录");

        recLv.setLayoutManager(new LinearLayoutManager(this));
        mHistoryAdapter = new HistoryAdapter();
        recLv.setAdapter(mHistoryAdapter);

    }

    @Override
    protected void loadData(int page, int pageCount) {
        ApiService.logview_article(this, new DialogCallback<LzyResponse<List<History>>>() {
            @Override
            public void onSuccess(Response<LzyResponse<List<History>>> response) {

                String time = "";
                boolean isFist = false;
                List<History> historyList = new ArrayList<>();

                for (int i = 0; i < response.body().data.size(); i++) {
                    History history = response.body().data.get(i);
                    Date format = null;
                    Date modified_time = null;
                    if (ObjectUtils.isNotEmpty(time)){
                        try {
                            format = new SimpleDateFormat("yyyy-MM-dd").parse(time);
                            modified_time = new SimpleDateFormat("yyyy-MM-dd").parse(history.modified_time);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        if(isSameDay(format, modified_time)){
                            if (ObjectUtils.isEmpty(history.histories)){
//                                historyList.add(history);
                                history.histories = new ArrayList<>();
                                history.histories.add(history);
                            }else {
                                history.histories.add(history);
                            }
                        }else{
                            if (ObjectUtils.isEmpty(history.histories)){
                                historyList.add(history);
                                history.histories = new ArrayList<>();
                                history.histories.add(history);
                            }else {
                                history.histories.add(history);
                            }
                        }
                    }else {
                        historyList.add(history);
                        if (ObjectUtils.isEmpty(history.histories)){
                            history.histories = new ArrayList<>();
                            history.histories.add(history);
                        }else {
                            history.histories.add(history);
                        }

                    }
                    time = history.modified_time;
                }

                onGetDataSuc(response.body().data);


            }
        });
    }

    @Override
    protected List<History> getData() {
        return mHistoryAdapter.getData();
    }

//    private void getData(){
//
//    }

    @Override
    protected void bindData(boolean isRefresh, List<History> list) {
        if (isRefresh){
            mHistoryAdapter.setNewData(list);
        }else {
            mHistoryAdapter.addData(list);
        }

    }

    public static boolean isSameDay(Date date1, Date date2) {
        if(date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    @SuppressLint("WrongConstant")
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    @Override
    protected void onRightClickAction() {
        super.onRightClickAction();
        new AlertDialog.Builder(this)
                .setMessage("是否清空浏览记录？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        ApiService.logview_delete_article(this, "all", new DialogCallback<LzyResponse<Object>>() {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
                                autoRefresh();
                            }
                        });

                    }
                }).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
